import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import Home from '../views/Home.vue'
import Index from '../views/Index.vue'
import Login from '../views/Login.vue'
import FrontHome from '../views/FrontHome.vue'
import qiantai from '../views/qiantai.vue'
import History from '../views/History'
import axios from "../axios";
import store from "../store"


const routes = [
    /*因为监听路由是否有改变，好像需要路由对象得有父路由，不然没响应...*/
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/index',
                name: 'Index',
                meta: {
                    title: "首页"
                },
                component: Index
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },

    {
        path: '/FrontHome',
        name: 'FrontHome',
        component: FrontHome,
        children: [
            {
                path: '/qiantai',
                name: 'qiantai',
                component: qiantai
            },
            {
                path: '/History',
                name: 'History',
                component: History
            },
            {
                path: '/Repassword',
                name: 'Repassword',
                meta: {
                    title: "修改密码"
                },
                component: ()=> import("@/views/sys/Repassword")
            },
        ]
    },
    {
        path: '/draw/:id',
        name: 'Draw',
        component: ()=> import("@/views/draw")
    },
    {
        path: '/drawInfo/:id',
        name: 'DrawInfo',
        component: ()=>import("@/views/DrawInfo")
    }
    // { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound, meta: {title: "404"} }

]


const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})


router.beforeResolve((to, from, next) => {
    let hasRoute = store.state.menus.hasRoute
    let token = localStorage.getItem("token")
    // let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3Nzc3NzciLCJpYXQiOjE2MzIxNTQzNDcsImV4cCI6MTYzMjc1OTE0N30.iWVkV7vP5eQR40hy1RTM-dIex15UONMvQC9izq5913PpeHfOZrG2k7nL7HUi4j2xgSgAyMKBBBVM5ByJoOxOmw"
    if (to.path == '/login' || to.name == 'DrawInfo') {
        console.log("go to login...")
        next()
    } else if (!token) {
        console.log("还没有token,请先登录")
        next({path: '/login'})
    } else if (to.path == '/' || to.path == '') {
        next({path: "/qiantai"})
    } else if (token && !hasRoute) {
        axios.get("/sys/menu/nav", {
            headers: {
                Authentication: localStorage.getItem("token")
            }
        }).then(res => {
            store.commit("setMenuList", res.data.data.nav)
            store.commit("setPermList", res.data.data.authoritys)


            // 这样动态添加菜单，只对符合全部都是两层的菜单才能这样写
            res.data.data.nav.forEach(menu => {
                if (menu.children) {
                    menu.children.forEach(e => {
                        let route = menuToRoute(e)
                        if (route) {
                            router.addRoute("Home", route)
                        }
                    })
                }
            })
            hasRoute = true
            store.commit("changeRouteStatus", hasRoute)
            next({...to, replace: true})
        })
    } else {
        next()
    }

})

const menuToRoute = (menu) => {
    if (!menu.component) {
        return null
    }
    let route = {
        name: menu.name,
        path: menu.path,
        meta: {
            icon: menu.icon,
            title: menu.title
        }
    }
    route.component = () => import("@/views/" + menu.component + ".vue")
    return route
}


export default router
